﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Lg.Core
{
    public  class Log
    {

        private static object locker = new object();
        public static int level = 4;
        /**
         * 向日志写入调试信息
         * @param className 类名
         * @param content 写入内容
         */
        public static void Debug(string title, string content, string filePreName = "Log")
        {
            if (level >= 3)
            {
                WriteLog("DEBUG", title, content, filePreName);
            }
        }

        /**
        * 向日志写入运行时信息
        * @param className 类名
        * @param content 写入内容
        */
        public static void Info(string title, string content, string filePreName = "Log")
        {
            if (level >= 2)
            {
                WriteLog("INFO", title, content, filePreName);
            }
        }

        /**
        * 向日志写入出错信息
        * @param className 类名
        * @param content 写入内容
        */
        public static void Error(string title, string content, string filePreName = "Log")
        {
            if (level >= 1)
            {
                WriteLog("ERROR", title, content, filePreName);
            }
        }
        /// <summary>
        /// 告警信息，任何时候都写入
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="filePreName"></param>

        public static void warning(string title, string content, string filePreName = "Log")
        {
            WriteLog("WARNING", title, content, filePreName);
        }

        /**
        * 实际的写日志操作
        * @param type 日志记录类型
        * @param className 类名
        * @param content 写入内容
        */
        protected static void WriteLog(string type, string title, string content, string filePreName)
        {
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间
            //日志内容
            string write_content = $"==============================［{time}－{type}－{title}］=================================================";
            write_content += "\r\n" + content + "\r\n";
            //需要用户自定义日志实现形式
            string path = AppContext.BaseDirectory + "\\Log\\";
            string _savePath = $"{path}{filePreName}_{DateTime.Now.ToString("yyyyMMdd")}.txt";
            lock (locker)
            {
                if (!Directory.Exists(path))//如果不存在就创建 dir 文件夹  
                {
                    Directory.CreateDirectory(path);
                }
                if (!File.Exists(_savePath))
                {
                    File.Create(_savePath).Close();
                }
                using (FileStream fs = new FileStream(_savePath, FileMode.Append))
                {
                    using (var writer = new StreamWriter(fs))
                    {
                        writer.WriteLine(write_content);
                    }
                }
            }
        }
    }
}
